package com.dimong.www.loan.ui.act;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

import com.dimong.loan.sdk.ui.act.BaseFragmentActivity;
import com.dimong.loan.sdk.utils.SharedUtil;
import com.dimong.www.loan.R;
import com.dimong.www.loan.ui.frag.GuidePageFrag;

import java.util.ArrayList;
import java.util.List;

public class GuidePageActivity extends BaseFragmentActivity implements OnPageChangeListener, OnClickListener {
    private ViewPager viewPager;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private GuidePagerAdapter pagerAdapter;

    public static Bitmap loadResBitmap(Context context, int id) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = 4;
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id);
        return bmp;
    }

    @Override
    public void initIntentParam(Intent intent) {
    }

    @Override
    public void beforeInitView() {

    }

    @Override
    public int getContentView() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.act_guide_page;
    }

    @Override
    public void initView() {
        viewPager = findViewById(R.id.guide_page_viewpager);
        mFragments.add(GuidePageFrag.getInstance(0));
        mFragments.add(GuidePageFrag.getInstance(1));
        mFragments.add(GuidePageFrag.getInstance(2));
        pagerAdapter = new GuidePagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void setViewStatus() {

    }

    @Override
    public void onActResume() {

    }

    @Override
    public void onActPause() {

    }

    @Override
    public void onActDestroy() {

    }

    @Override
    public void onActRestart() {

    }

    @Override
    public void onActStop() {

    }

    @Override
    public void onActStart() {

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int position) {
//        switch (position) {
//            case 2:
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        finishActivity();
//                    }
//                }, 2000);
//                break;
//            default:
//                break;
//        }
    }

    @Override
    public void onClick(View arg0) {
        finishActivity();
    }

    public void finishActivity() {
        SharedUtil.saveIsFirstOpen(false);
        Intent intent = new Intent(this, MainActivity.class);
        this.startActivity(intent);
        finish();
    }

    public class GuidePagerAdapter extends FragmentPagerAdapter {
        public GuidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        public Fragment getItem(int arg0) {
            return mFragments.get(arg0);
        }
    }
}
